//
//  LSOrginalView.swift
//  新浪微博---01次
//
//  Created by 李宗帅 on 16/5/7.
//  Copyright © 2016年 LS. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
let LSOrginalViewMargin: CGFloat = 10
class LSOrginalView: UIView {
    
    var picViewConstrain : Constraint?
    
    var orginalViewModel:LSStatusViewModel?{
        didSet{
            nameLabel.text = orginalViewModel?.status?.user?.screen_name
//            sourceLabel.text = orginalViewModel?.status?.source
            
            sourceLabel.text = orginalViewModel?.source_string
            
            timeLabel.text = orginalViewModel?.createat_string
            
//            contentLabel.text = orginalViewModel?.status?.text
            //能正常显示表情
            contentLabel.attributedText = orginalViewModel?.dealAttributeString(orginalViewModel?.status?.text)
            
            levelImageView.image = orginalViewModel?.levelImage
            
            verfityImageView.image = orginalViewModel?.verifityImage
            
            photoImageView.sd_setImageWithURL(orginalViewModel?.profile_url)
            
            //存在约束重用的问题  所以先把约束卸了 再从新添加
            picViewConstrain?.uninstall()
            
            if let count = orginalViewModel?.status?.pic_urls?.count where count == 0 {
                //没有配图
                picView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    picViewConstrain = make.bottom.equalTo(contentLabel).constraint
                })
            } else {
                //有配图
                picView.hidden = false
                
                picView.pic_urls = orginalViewModel?.status?.pic_urls

                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    picViewConstrain = make.bottom.equalTo(picView).constraint
                })
                
                
    
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI(){
        
        //添加
        addSubview(photoImageView)
        addSubview(nameLabel)
        addSubview(levelImageView)
        addSubview(verfityImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(picView)
        
        //设置约束
        //头像
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(LSOrginalViewMargin)
            
            make.left.equalTo(self.snp_left).offset(LSOrginalViewMargin)
            
            make.width.height.equalTo(30)
        }
        //昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(LSOrginalViewMargin)
            
            make.top.equalTo(photoImageView.snp_top)
        }
        //等级
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel.snp_centerY)
            make.left.equalTo(nameLabel.snp_right).offset(LSOrginalViewMargin)
        }
        //认证
        verfityImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
        }
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(LSOrginalViewMargin)
            
            make.bottom.equalTo(photoImageView.snp_bottom)
        }
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel.snp_centerY)
            make.left.equalTo(timeLabel.snp_right).offset(LSOrginalViewMargin)
        }
        //内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
             make.top.equalTo(photoImageView.snp_bottom).offset(LSOrginalViewMargin)
            
            make.right.equalTo(self.snp_right)
        }
        
        //配图视图
        picView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            
            make.top.equalTo(contentLabel.snp_bottom).offset(LSOrginalViewMargin)
            
//            make.width.height.equalTo(100)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            picViewConstrain = make.bottom.equalTo(picView.snp_bottom).constraint
        }
    }
    
    //MARK: --懒加载控件
    //头像
    private lazy var photoImageView:UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        return img
        
    }()
    
    //认证
    private lazy var verfityImageView:UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_vip")
        
        return img
        
    }()
    
    //等级
    private lazy var levelImageView:UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "common_icon_membership_level1")
        
        return img
        
    }()
    
    //用便利构造函数  定义了一个相当于OC里的系统类的分类
    //昵称
    private lazy var nameLabel:UILabel = UILabel(size: 15, color: UIColor.lightGrayColor(), singleLine: true)
    //时间
    private lazy var timeLabel:UILabel = UILabel(size: 13, color: UIColor.orangeColor(), singleLine: true)
    //来源
    private lazy var sourceLabel:UILabel = UILabel(size: 15, color: UIColor.darkGrayColor(), singleLine: true)
    
    //内容
    private lazy var contentLabel:UILabel = UILabel(size: 15, color: UIColor.darkGrayColor(), singleLine: false)
    
    //配图
    private lazy var picView: LSPhotoView = LSPhotoView()
}
